package dhee.wtqshopproject.utils;

import dhee.wtqshopproject.dao.UserMapper;
import dhee.wtqshopproject.entity.User;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class UserMapperUtils {

    private static UserMapperUtils userMapperUtils;

    @Resource
    private UserMapper userMapper;

    @PostConstruct
    public void init() {
        userMapperUtils = this;
        userMapperUtils.userMapper = this.userMapper;
    }
    //查询
    public static List<User> findall(){
        List<User> users = userMapperUtils.userMapper.selectAll();
        System.out.println("users = " + users);
         return users;
    }
    //查询
    public static User findById(int id){return userMapperUtils.userMapper.selectById(id);}
    public static List<User> findByUsername(String username){return userMapperUtils.userMapper.selectByUsername(username);}
   //更改
    public static void updateUser(User user){
        userMapperUtils.userMapper.updateUser(user);
   }
   //增加
    public static void insertUser(User user){
        List<User> user1 = userMapperUtils.userMapper.selectByUsername(user.getUsername());
        if(user1 ==null){
        userMapperUtils.userMapper.insertUser(user);}
    }

    //删除
    public static void deleteUser(int id){
        userMapperUtils.userMapper.deleteById(id);
    }
public  static List<User> selectLike(String keyword){
        List<User> users = userMapperUtils.userMapper.selectLike(keyword);
        return users;
}
//登录
    public static  void login(User user){
         userMapperUtils.userMapper.login(user);
         //保存用户到线程判断
         UserContext.setCurrentUser(user);
        System.out.println("login");
    }
    //注册
    public static void zhuceusername(User user){
        userMapperUtils.userMapper.zhuceUser(user);
    }
}
